package com.yingxin.yxerp.module.report.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yx.framework.commons.enums.DynamicTaskSetTypeEnums;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yxerp.module.report.entity.DynamicTaskSet;
import com.yingxin.yxerp.module.report.mapper.DynamicTaskSetMapper;
import com.yingxin.yxerp.module.report.service.IDynamicTaskSetService;
import com.yingxin.yxerp.module.report.vo.resp.DynamicTaskSetRespVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 定时任务 地址设置 服务类 实现类
 * @author yuebao
 * @date 2023-12-08
 */

@Service
public class DynamicTaskSetServiceImpl
        extends ServiceImpl<DynamicTaskSetMapper, DynamicTaskSet>
        implements IDynamicTaskSetService {

    /**
     * 获取 mapper
     */
    @Override
    public DynamicTaskSetMapper mapper(){
        return baseMapper;
    }

    /**
     * 保存
     */
    @Override
    public void saveSet( DynamicTaskSet set ){
        // 验证
        checkInfo( set );

        save( set );
    }

    private void checkInfo( DynamicTaskSet log ){
        if( ObjectUtil.isEmpty( log ) ){
            throw new BizException("存储对象不能为空！");
        }
        if(StringUtils.isEmpty( log.getTaskType() ) ){
            throw new BizException("类型不能为空！");
        }
        if(StringUtils.isEmpty( log.getTaskUrl() ) ){
            throw new BizException("URL不能为空！");
        }

        DynamicTaskSetRespVo vo = baseMapper.getOneVo( DynamicTaskSetTypeEnums.TASK_URL.getType() );
        if( ObjectUtil.isNotEmpty( vo ) ){
            throw new BizException("已经存在同类型的设置！");
        }
    }

    /**
     * 修改
     */
    @Override
    public void updateSet(DynamicTaskSet set ){
        DynamicTaskSet updateSet = getById( set.getId() );
        updateSet.setTaskUrl( set.getTaskUrl() );
        updateSet.setRemark( set.getRemark() );
        updateById( updateSet );
    }

}
